# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA27_28_fullsamp_prep.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat1920.Race.WeekWeight.rdata")
load("./data/impdat20.Race.WeekWeight.rdata")


# Equalizing weeks with Nationscape
impdat20.WeekWeight$week2 <- as.numeric(impdat20.WeekWeight$week) + 1
impdat20.WeekWeight$week2[which(impdat20.WeekWeight$week2 == 53)] <-  52
impdat20.WeekWeight$week2 <- as.character(impdat20.WeekWeight$week2)
impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)] <- paste0("0", impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)])
impdat20.WeekWeight <- fastDummies::dummy_cols(impdat20.WeekWeight, select_columns = "week2")

impdat20.WeekWeight$week2_post <- "Pre-GF"
for(i in 22:52){
  impdat20.WeekWeight$week2_post[which(impdat20.WeekWeight[, paste0("week2_", i)] == 1)] <- paste("Week", i)
}

vars <- c("year", "week", "date_mdy", "ideo_mod", "ideo_con", "age_cat4", "edu_cat4", "race5", "sex",
          "census_region", "broughtwebsite2", "WeekWeights", "t_wb")
impdat19_sub <- subset(impdat1920.WeekWeight, select = vars)
impdat20_sub <- subset(impdat20.WeekWeight, select = vars)

impdat_comb <- rbind(impdat19_sub, impdat20_sub)


# Models ------------------------------------------------------------------
m_week1920 <- lm(t_wb ~ as.factor(week)*year + ideo_mod + ideo_con +
                      age_cat4 + edu_cat4 + race5 + sex +
                      census_region + broughtwebsite2,
                    data = impdat_comb, weights = WeekWeights,
                    subset = date_mdy != "2020-05-25")
summary(m_week1920)

# full pre-post
m1_prepost <- lm(t_wb ~ post_gf +
                   ideo_mod + ideo_con +
                   age_cat4 + edu_cat4 + race5 + sex +
                   census_region + broughtwebsite2,
                 data = impdat20.WeekWeight, weights = WeekWeights)
summary(m1_prepost)


# Weekly after
m1_pre_weeklypost <- lm(t_wb ~ week2_post +
                          ideo_mod + ideo_con +
                          age_cat4 + edu_cat4 + race5 + sex +
                          census_region + broughtwebsite2,
                        data = impdat20.WeekWeight, weights = WeekWeights)
summary(m1_pre_weeklypost)


# Prediction Data Set -----------------------------------------------------
weeks <- names(table(m_week1920$model$`as.factor(week)`))
pdat <- data.frame(week = weeks,
                   year = c(rep(2019, length(weeks)), rep(2020, length(weeks))),
                   ideo_mod = 0,
                   ideo_con = 0,
                   race5 = "White",
                   age_cat4 = "18-29",
                   edu_cat4 = "Post-Grad",
                   sex = "f",
                   census_region = "South",
                   broughtwebsite2 = "Assignment school/work")


# Plot --------------------------------------------------------------------
preds <- predict(m_week1920, pdat, se.fit = T)

p_dat <- data.frame(year = c(rep("2019", length(weeks)), rep("2020", length(weeks))),
                    week = weeks,
                    pred = preds$fit,
                    se = preds$se.fit)
p_dat$week <- as.Date(paste(2020, p_dat$week, 1, sep="-"), "%Y-%U-%u")
p_dat$week[c(1,54)] <- "2020-01-01" # from 00 weeks

full_1920_therm <- ggplot(p_dat, aes(x = week, y = pred, group = year)) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .75) +
  geom_smooth(data = subset(p_dat, week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  geom_smooth(data = subset(p_dat, week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  theme_bw() +
  scale_color_manual(values = c("grey", "black"), name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "") +
  scale_x_date(breaks = unique(p_dat$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())

p_dat_Fsamp_therm <- p_dat

# Pre-Post Regressions ----------------------------------------------------
prepost <- summary(m1_prepost)
weekly <- summary(m1_pre_weeklypost)
week_names <- grep("week", rownames(weekly$coefficients), value = T)
coef_out <- array(NA, c(length(week_names)+1,3))
coef_out <- as.data.frame(coef_out)
names(coef_out) <- c("var", "coef", "se")
coef_out[1,"var"] <- "Post GF, May 26, 2020-Dec 31, 2020"
coef_out[1,c("coef", "se")] <- prepost$coefficients[2, c("Estimate", "Std. Error")]
coef_out[-1, "var"] <- gsub("week2_post", "", week_names)
coef_out[-1,c("coef", "se")] <- weekly$coefficients[week_names,c("Estimate", "Std. Error")]
coef_out$var <- as.factor(coef_out$var)
coef_out$var <- factor(coef_out$var,
                       levels = rev(levels(coef_out$var)))
coef_out$indx <- c(1, rep(0, length(week_names)))
coef_out$indx <- ifelse(coef_out$indx == 1, "Pre-Post", "Weekly Indicators")

coef_out_Fsamp <- coef_out

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()